/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.richard.base.net.interceptors

import com.richard.base.net.utils.Utils
import okhttp3.HttpUrl
import java.util.*

class GlobalUrlManager {

    private val mDomainNameHub: MutableMap<String, HttpUrl> = HashMap()

    @get:Synchronized
    var mDomain: MutableMap<String, String> = HashMap()

    fun setGlobalDomain(globalDomain: String) {
        Utils.checkNotNull(globalDomain, "globalDomain cannot be null")
        synchronized(mDomainNameHub) {
            mDomainNameHub.put(
                GLOBAL_DOMAIN_NAME,
                Utils.checkUrl(globalDomain)
            )
        }
    }

    fun removeGlobalDomain() {
        synchronized(mDomainNameHub) {
            mDomainNameHub.remove(GLOBAL_DOMAIN_NAME)
        }
    }

    @get:Synchronized
    val globalDomain: HttpUrl?
        get() = mDomainNameHub[GLOBAL_DOMAIN_NAME]


    companion object {
        private const val GLOBAL_DOMAIN_NAME = "com.ks"

        const val IDENTIFICATION_IGNORE = "#url_ignore"

        const val DOMAIN = "ksdomain"

        val instance: GlobalUrlManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            GlobalUrlManager()
        }

    }
}